<%--
  Author: Al-assad 余林颖
  E-mail: yulinying_1994@outlook.com
  Date: 2018/1/10 1:00 
  Description:
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>websocket 测试</title>
    <script type="text/javascript">
        //创建 websocket
        var url = 'ws://'+ window.location.host + '<%= request.getContextPath() %>/hello';
        var sock = new WebSocket(url);
        //websocket 连接行为
        sock.onopen = function(){
            console.log('开启 websocket 连接');
            sayHello();
        };
        //websocket 接受到信息行为
        sock.onmessage = function(e){
            console.log('接受信息',e.data);
            setTimeout(function(){sayHello()},2000);
        };
        //websocket 关闭行为
        sock.onclose = function(){
            console.log('关闭 websocket 连接');
        };

        function sayHello() {
            console.log('发送信息：hello world!');
            sock.send('form client: Hello world!');
        }
    </script>
</head>
<body>
Hello world!
</body>
</html>
